package com.betterjr.modules.profit.service;

import com.alibaba.fastjson.JSONObject;
import com.betterjr.common.service.BaseService;
import com.betterjr.mapper.pagehelper.Page;
import com.betterjr.modules.accountSupervision.service.ScfWarnService;
import com.betterjr.modules.profit.constants.LoanShareConstant;
import com.betterjr.modules.profit.dao.ScfLoanShareMapper;
import com.betterjr.modules.profit.entity.ScfLoanShare;
import com.betterjr.modules.risk.loan.field.ConfigFieldCollections;
import com.betterjr.modules.risk.loan.util.FutureUtil;
import com.betterjr.modules.risk.loan.util.HttpRequestBuilder;
import com.betterjr.modules.risk.loan.util.HttpRequestObject;
import com.betterjr.modules.risk.loanmanage.service.LoanDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenlj on 2018/6/27
 */
@Service
public class ScfLoanShareService extends BaseService<ScfLoanShareMapper, ScfLoanShare> {

    @Autowired
    private ScfWarnService scfWarnService;

    @Autowired
    private LoanDataService loanDataService;

    @Value("${web.prefix.fund.system.url}")
    private String prefixFundSystemUrl;

    public Page<ScfLoanShare> queryLoanShareList(Map<String, Object> anMap, int anFlag, int anPageNum, int anPageSize) {
        anMap.remove("flag");
        anMap.remove("pageNum");
        anMap.remove("pageSize");
        anMap.remove("pages");
        anMap.remove("total");

        /**如果核心企业ID为空，则查询全部企业的分润费*/
        if (anMap.get("coreno")==null || ((String) anMap.get("coreno")).equals("")) {
            anMap.remove("coreno");
        }
        anMap.put("shareType", LoanShareConstant.SHARE_TYPE_CORE);
        Page<ScfLoanShare> list = this.selectPropertyByPage(anMap, anPageNum, anPageSize, 1 == anFlag);

        return list;
    }

    public int saveConfirmLoanShareStatus(Map<String, Object> anMap) {
        //TODO
        Long id = Long.parseLong((String) anMap.get("id"));
        String paydate = (String) anMap.get("paydate");
        String paytype = (String) anMap.get("paytype");
        String paytime = (String) anMap.get("paytime");
        String description = (String) anMap.get("description");
        ScfLoanShare scfLoanShare = this.selectByPrimaryKey(id);
        scfLoanShare.setBusinstatus(LoanShareConstant.BUSINSTATUS_PREPAID);
        scfLoanShare.setPaydate(paydate);
        scfLoanShare.setPaytype(paytype);
        scfLoanShare.setPaytime(paytime);
        scfLoanShare.setDescription(description);

        /** 分润主体为运营方的分润状态未修改*/
        ScfLoanShare scfLoanShareQuery = new ScfLoanShare();
        scfLoanShareQuery.setServiceId(scfLoanShare.getServiceId());
        List<ScfLoanShare> selectResult = this.select(scfLoanShareQuery);
        ScfLoanShare scfLoanSharePlatform = selectResult.get(0);
        scfLoanSharePlatform.setBusinstatus(LoanShareConstant.BUSINSTATUS_PREPAID);
        scfLoanSharePlatform.setPaydate(paydate);
        scfLoanSharePlatform.setPaytype(paytype);
        scfLoanSharePlatform.setPaytime(paytime);
        scfLoanSharePlatform.setDescription(description);
        this.updateByPrimaryKeySelective(scfLoanSharePlatform);
        return this.updateByPrimaryKeySelective(scfLoanShare);
    }

    public HashMap<String, Object> queryFundingPartyIndexData(Map<String, Object> anMap) {
        String fundId = (String) anMap.get("funId");
//        String fundId = "102202019";
        HashMap<String, Object> result = new HashMap<>();

        List<Map<String, Object>> warnCount = scfWarnService.getWarnCountByProviderId(fundId);
        Map<String, Object> warnCountMap0 = warnCount.get(0);
        Object oneWeekReceipt = warnCountMap0.get("count");
        Map<String, Object> warnCountMap1 = warnCount.get(1);
        Object alreadyOverdue = warnCountMap1.get("count");
        Map<String, Object> warnCountMap2 = warnCount.get(2);
        Object balance = warnCountMap2.get("count");
        Map<String, Object> warnCountMap3 = warnCount.get(3);
        Object principal = warnCountMap3.get("count");
        Map<String, Object> warnCountMap4 = warnCount.get(4);
        Object abnormal = warnCountMap4.get("count");

        result.put("oneWeekReceipt", oneWeekReceipt);
        result.put("alreadyOverdue", alreadyOverdue);
        result.put("balance", balance);
        result.put("principal", principal);
        result.put("abnormal", abnormal);

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("fundProviderId", fundId);
        List<Map<String, String>> loanData = loanDataService.homeReportCountData(paramMap);
        Map<String, String> loanDataTempMap0 = loanData.get(0);
        String alreadyLoanAmount = loanDataTempMap0.get("已放款总额");
        String alreadyLoanNum = loanDataTempMap0.get("已放款总笔数");
        Map<String, String> loanDataTempMap1 = loanData.get(1);
        String shouldReceiptAmount = loanDataTempMap1.get("应收款总额");
        String shouldReceiptNum = loanDataTempMap1.get("应收款总笔数");
        Map<String, String> loanDataTempMap2 = loanData.get(2);
        String alreadyReceiptAmount = loanDataTempMap2.get("已收款总额");
        String alreadyReceiptNum = loanDataTempMap2.get("已收款总笔数");
        Map<String, String> loanDataTempMap3 = loanData.get(3);
        String alreadyOverdueAmount = loanDataTempMap3.get("已逾期总额");
        String alreadyOverdueNum = loanDataTempMap3.get("已逾期总笔数");

        result.put("alreadyLoanAmount", alreadyLoanAmount);
        result.put("alreadyLoanNum", alreadyLoanNum);
        result.put("shouldReceiptAmount", shouldReceiptAmount);
        result.put("shouldReceiptNum", shouldReceiptNum);
        result.put("alreadyReceiptAmount", alreadyReceiptAmount);
        result.put("alreadyReceiptNum", alreadyReceiptNum);
        result.put("alreadyOverdueAmount", alreadyOverdueAmount);
        result.put("alreadyOverdueNum", alreadyOverdueNum);


        /**应收账款对账提醒*/
        Map<String, Object> tradeParamMap = new HashMap<>();
        tradeParamMap.put("fundId", fundId);
        HttpRequestObject requestObjectTrade = createRequest(tradeParamMap,
                ConfigFieldCollections.REQUEST_HTTP_METHOD_POST,
                ConfigFieldCollections.API_TRADE_QUERYNOTCONFIRMNUM);
        FutureUtil.getHttpFuture(requestObjectTrade);
        String httpResultTrade = FutureUtil.getHttpResult(requestObjectTrade);

        /**白名单审核提醒*/
        Map<String, Object> whitelistParamMap = new HashMap<>();
        whitelistParamMap.put("fundId", fundId);
        HttpRequestObject requestObjectWhitelist = createRequest(whitelistParamMap,
                ConfigFieldCollections.REQUEST_HTTP_METHOD_POST,
                ConfigFieldCollections.API_WHITELIST_GETNUMBER);
        FutureUtil.getHttpFuture(requestObjectWhitelist);
        String httpResultWhitelist = FutureUtil.getHttpResult(requestObjectWhitelist);

        result.put("trade", httpResultTrade);             //应收账款对账提醒
        result.put("whitelist", httpResultWhitelist);     //白名单审核提醒

        return result;
    }

    /**
     *
     * @Title: createRequest
     * @Description: 创建和前置系统连接的对象
     * @param anParamMap  请求参数json
     * @param requestType  请求方法方式
     * @param apiUrl  发送httpapi路径
     * @return
     * @throws
     * @author xuyp
     * @date 2018年6月25日 下午5:29:34
     */
    private HttpRequestObject createRequest(Map<String, Object> anParamMap, String requestType, String apiUrl) {
        return HttpRequestBuilder.newInstance().url(prefixFundSystemUrl + apiUrl)
                .paramObject(JSONObject.toJSONString(anParamMap.values())).requestType(requestType).builder();
    }


}
